﻿using Abp.Authorization;
using shenghua.Authorization;
using shenghua.Authorization.Users;
using shenghua.Users.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Users
{
    public class AdminAppService: shenghuaAppServiceBase, IAdminAppService
    {
        private readonly UserManager _userManager;

        public AdminAppService(
            UserManager userManager
            )
        {
            _userManager = userManager;
        }

        [AbpAuthorize(PermissionNames.ChangePassword)]
        public async Task ChangePassword(ChangeAdminPasswordDto input)
        {
            var user = await GetCurrentUserAsync();
            await _userManager.ChangePasswordAsync(user, input.currentPassword, input.newPassword);
        }

        [AbpAuthorize(PermissionNames.PersonalProfile)]
        public async Task<AdminPersonalProfileDto> GetPersonalProfile()
        {
            var user = await GetCurrentUserAsync();
            var roles = await _userManager.GetRolesAsync(user);
            return new AdminPersonalProfileDto(user, roles.Count > 0 ? roles[0] : "");
        }

        [AbpAuthorize(PermissionNames.PersonalProfile)]
        public async Task UpdatePersonalProfile(Dto.UpdateAdminPersonalProfileDto input)
        {
            var user = await GetCurrentUserAsync();
            input.MapTo(user);
            await _userManager.UpdateProsonalProfile(user);
        }
    }
}
